﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

using Perseus;
using Perseus.Data;

using Hetymine.Executioner.Data;

namespace Hetymine.Executioner.Plugins.Commands.FTP {
    /// <summary>
    /// Interaction logic for FTPConfigurator.xaml
    /// </summary>
    public partial class FTPConfigurator : UserControl, IConfigurator<IniFile> {
        private IniFile _Config;
        private bool _IsApply = false;

        public FTPConfigurator() {
            InitializeComponent();

            this.cImageFilename.TextChanged += new TextChangedEventHandler(Filename_TextChanged);
            this.cTextFilename.TextChanged += new TextChangedEventHandler(Filename_TextChanged);
        }

        private void Filename_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e) {
            if (!this._IsApply) { return; }

            TextBox t = sender as TextBox;

            string filename = PIO.RemoveInvalidFileNameChars(t.Text);

            if (filename != t.Text) {
                t.Background = Brushes.LightPink;
            }
            else {
                t.Background = SystemColors.ControlLightLightBrush;

                switch (t.Name) {
                    case "cImageFilename":
                        this._Config.SetString("FTP", "Default Image Filename", t.Text.Trim());
                        break;
                    case "cTextFilename":
                        this._Config.SetString("FTP", "Default Text Filename", t.Text.Trim());
                        break;
                }
            }
        }

        #region IConfigurator<IniFile> Members
        public void InitializeConfig(IniFile config) {
            this._Config = config;

            this._IsApply = false;

            this.cImageFilename.Text = this._Config.GetString("FTP", "Default Image Filename", "clipboard");
            this.cTextFilename.Text = this._Config.GetString("FTP", "Default Text Filename", "clipboard");

            this._IsApply = true;
        }
        public UserControl ConfigPanel { get { return this; } }        
        #endregion
    }
}
